package com.roadjava.favorite.mapper;


import com.roadjava.favorite.bean.entity.FavoriteDO;
import com.roadjava.favorite.bean.entity.FavoriteEQ;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface FavoriteMapper {

    // 添加收藏
    @Insert("INSERT INTO favorite (ResourceID, id, FavoriteStatus) VALUES (#{resourceId}, #{Id}, #{favoriteStatus})")
    void addFavorite(FavoriteEQ favorite);

    // 删除收藏
    @Delete("DELETE FROM favorite WHERE FavoriteID = #{favoriteId}")
    void removeFavorite(@Param("favoriteId") Integer favoriteId);

    // 查询用户的所有收藏
    @Select("SELECT * FROM favorite WHERE id = #{userId}")
    List<FavoriteDO> getUserFavorites(@Param("userId") Long Id);

    // 查询指定资源的收藏数量
    @Select("SELECT COUNT(*) FROM favorite WHERE ResourceID = #{resourceId} AND FavoriteStatus = 1")
    Long getFavoriteCount(@Param("resourceId") Long resourceId);

    // 删除特定资源的所有收藏
    @Delete("DELETE FROM favorite WHERE ResourceID = #{resourceId}")
    void removeFavoritesByResource(@Param("resourceId") Long resourceId);

    @Delete("DELETE FROM favorite WHERE ResourceID = #{resourceId} AND id = #{userId}")
    void removeFavoriteByResourceAndUser(@Param("resourceId") Long resourceId, @Param("userId") Long userId);

    @Select("SELECT COUNT(*) FROM favorite WHERE ResourceID = #{resourceId} AND id = #{Id}")
    int isFavoritedByUser(@Param("resourceId") Long resourceId, @Param("Id") Long Id);
    ;
}
